package com.woniuxy.enjoylife.controller;

import com.woniuxy.enjoylife.entity.Recent;

import com.woniuxy.enjoylife.entity.dto.RecentDetailDto;
import com.woniuxy.enjoylife.entity.dto.RecentInfoDto;
import com.woniuxy.enjoylife.entity.dto.RecentDto;
import com.woniuxy.enjoylife.entity.dto.RecentUserDto;
import com.woniuxy.enjoylife.enums.StateEnum;
import com.woniuxy.enjoylife.service.RecentService;
import com.woniuxy.enjoylife.service.UserService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.ResponseResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/recent")
public class RecentController {

    @Resource
    private RecentService recentServiceImpl;
    @Resource
    private HttpServletRequest request;
    @Resource
    private UserService userService;

    //新增
    @RequestMapping("/saverecent")
    public boolean addRecent(Recent recent) {
        int userid=(int) request.getSession().getAttribute("id");
        recent.setUid(userid);
        recent.setTime(LocalDateTime.now());
        return recentServiceImpl.addRecent(recent,userid);
    }

    //修改
    @RequestMapping("/updaterecent")
    public ResponseResult updateRecent(@RequestBody Recent recent) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        recentServiceImpl.updateRecent(recent);
        return responseResult;
    }

    //删除
    @RequestMapping("/deleterecent")
    public ResponseResult deleteRecent(int id) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        recentServiceImpl.deleteRecent(id);
        return responseResult;
    }


    //查询所有动态
    @RequestMapping("/selectRecent")
    public PageResult<RecentDto> selectRecent(int page, int size) {
        int userid = (int) request.getSession().getAttribute("id");
        return recentServiceImpl.selectRecent(page, size, userid);
    }

    //查找单个用户动态
    public Recent selectSingle(int uid){

        return null;
    }

    //***********************用户角度****************8

    /**
     * 用户参与的动态
     * @return
     */
    @RequestMapping("/selectUserRecent")
    @ResponseBody
    public ResponseResult<List<RecentUserDto>>  selectUserRecent(){
        //用户uid
        int uid = (int) request.getSession().getAttribute("id");
        ResponseResult<List<RecentUserDto>> responseResult =new ResponseResult<>();

        List<RecentUserDto> recentUserDtos = recentServiceImpl.selectUserRecent(uid);
        for (RecentUserDto r:recentUserDtos) {
            r.setUser(userService.findAuthor(r.getRid()));
        }
        if(recentUserDtos.size()>0){
            responseResult.success();
        }else {
            responseResult.error();
        }
        responseResult.setData(recentUserDtos);

        return responseResult;
    }

    /**
     * 平台查找动态
     * @param type
     * @param content
     * @return
     */
    @RequestMapping("/findType")
    public ResponseResult findType(int type,String content){
        ResponseResult<List<RecentInfoDto>> responseResult = new ResponseResult<>();
        responseResult.setStatusCode(200);
        responseResult.setState(StateEnum.SUCCESS);
        responseResult.setData(recentServiceImpl.findType(type,content));
        return responseResult;
    }

    /**
     * 平台删除动态
     * @param id
     * @return
     */
    @RequestMapping("/delRecent")
    public ResponseResult delRecent(int id){
        ResponseResult<Object> responseResult = new ResponseResult<>();
        if(recentServiceImpl.delRecent(id)){
            responseResult.setStatusCode(200);
            responseResult.setState(StateEnum.SUCCESS);
        }else{
            responseResult.setStatusCode(501);
            responseResult.setState(StateEnum.ERROR);
            responseResult.setMessage("删除失败，请稍后重试！");
        }
        return responseResult;
    }



    /**
     * 模糊插叙的动态
     * @param content
     * @return
     */
    @RequestMapping("/findRecentByLike")
    @ResponseBody
    public ResponseResult<PageResult> findRecentByLike(String content,int current, int size){
        System.out.println(content+current+size);
        ResponseResult responseResult = new ResponseResult();
        PageResult<RecentDto> pageResult = recentServiceImpl.findRecentByLike(content,current,size);
        if(pageResult == null){
            responseResult.error();
        }else{
            responseResult.success();
            responseResult.setData(pageResult);
        }
        return responseResult;
    }


}
